package com.jsgt.greekfire.ui.wallet

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.fhx.core.base.BaseActivity
import com.fhx.core.goToActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import com.jsgt.greekfire.ui.wallet.adapter.WalletAdapter
import com.jsgt.greekfire.ui.wallet.adapter.WalletManageData
import com.jsgt.greekfire.ui.wallet.add.AddWalletActivity
import kotlinx.android.synthetic.main.activity_manage.*

class ManageActivity : BaseActivity(), View.OnClickListener {
    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(this).get(WalletViewModel::class.java)
    }

    private val data: MutableList<WalletManageData> by lazy {
        ArrayList<WalletManageData>()
    }

    private val walletAdapter: WalletAdapter by lazy {
        WalletAdapter()
    }
    private var wallet: IdentityWallet? = null

    private var isAddTitle = false

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_manage
    }

    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)
        title = getString(R.string.manage_title)
        imvAddWallet.setOnClickListener(this)

        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = walletAdapter
        walletAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.txvManager) {
                goToActivity<WalletIdentityManagerActivity> {
                    putExtra("wallet", wallet)
                }
            }
        }
        walletAdapter.setNewInstance(data)
        walletAdapter.setOnItemClickListener { adapter, view, position ->
            if (adapter.getItemViewType(position) == WalletManageData.VIEW_TYPE_WALLET) {
                if (walletAdapter.getItem(position).data?.identityName.isNullOrEmpty()){
                    goToActivity<WalletManagerImportActivity> {
                        this.putExtra("wallet", walletAdapter.getItem(position).data)
                    }
                }else {
                    goToActivity<WalletManagerActivity> {
                        this.putExtra("wallet", walletAdapter.getItem(position).data)
                    }
                }
            } else if (adapter.getItemViewType(position) == WalletManageData.VIEW_TYPE_ADD_COIN) {
                ToastUtils.show(R.string.not_yet_open)
            }
        }

        walletViewModel.getAllIdentityWallet()?.observe(this, Observer {
            data.clear()
            data.add(WalletManageData(WalletManageData.VIEW_TYPE_MANAGE_TITLE, null))
            isAddTitle = false
            wallet = it[0]
            it.forEach { it1 ->
                if (it1?.identityName.isNullOrEmpty()) {
                    if (!isAddTitle) {
                        data.add(WalletManageData(WalletManageData.VIEW_TYPE_ADD_COIN, null))
                        data.add(WalletManageData(WalletManageData.VIEW_TYPE_ADD_TITLE, null))
                        isAddTitle = true
                    }
                }
                data.add(WalletManageData(WalletManageData.VIEW_TYPE_WALLET, it1))
            }
            if (!isAddTitle) {
                data.add(WalletManageData(WalletManageData.VIEW_TYPE_ADD_COIN, null))
            }
            walletAdapter.notifyDataSetChanged()
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.imvAddWallet -> {
                goToActivity<AddWalletActivity>()
//                ToastUtils.show(R.string.not_yet_open)
            }
        }
    }
}